package com.example.cmpe277stockdata;

import java.util.List;

import com.example.cmpe277stockdata.beans.NewsBean;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class NewsAdapter extends ArrayAdapter<NewsBean>
{
	private int resourceId;
    private LayoutInflater inflater;
    private Context ctx;
    private List<NewsBean> resultData;
    
	public NewsAdapter(Context context, int resource,List<NewsBean> objects) {
		super(context, resource, objects);
		
		resourceId = resource; 
		inflater = LayoutInflater.from(context);
		ctx = context;
		resultData = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		//return super.getView(position, convertView, parent);
		
		Log.d("NewsAdapter", "Inside getView function");
		
		/* create a new view of my layout and inflate it in the row */
        convertView = (RelativeLayout) inflater.inflate( resourceId, null );

        /* Extract the city's object to show */
        final NewsBean newsData = resultData.get( position );
        
        if(newsData == null)
        {
        	return null;
        }
       
        // Take the TextView from layout and set the news Description 
        TextView txtNewsTitle = (TextView) convertView.findViewById(R.id.newsTitle);
        txtNewsTitle.setText(newsData.getNewsTitle());
        txtNewsTitle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				//Open News Data in the Browser
				Intent browserIntent = new Intent(Intent.ACTION_VIEW,
							Uri.parse(newsData.getNewsLink()));
				ctx.startActivity(browserIntent);
			}
		});
        
        // Take the TextView from layout and set the news Link
        TextView txtPubDate = (TextView) convertView.findViewById(R.id.pubDate);
        txtPubDate.setText( newsData.getPubDate());
       
        Log.d("NewsAdapter", "Finished getView function");
        return convertView;
	}
	
	

}
